package com.hippo.core.vm

import android.content.Context
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hippo.core.BaseApplication
import com.hippo.core.R
import com.hippo.core.core.ext.toJson
import com.hippo.core.core.manager.AppStore
import com.hippo.core.respository.ApiRepository
import com.hippo.core.state.DialogType
import com.hippo.core.tuya.TuYaManager
import com.hippo.core.state.HomeState
import com.thingclips.smart.android.ble.api.ScanDeviceBean
import com.thingclips.smart.sdk.bean.DeviceBean
import kotlinx.coroutines.launch

class HomeVM : ViewModel() {

    var uiState by mutableStateOf(HomeState())
        private set


    fun setDeviceWarn(devId: String){
        viewModelScope.launch {
            ApiRepository.setDeviceWarn(devId)
                .onSuccess {
                }
        }
    }

    fun dismissDialog() {
        uiState = uiState.copy(dialogType = DialogType.Idle)
    }


    fun updateState(state: HomeState) {
        uiState = state
    }


    fun getBasicData() {
        viewModelScope.launch {
            ApiRepository.getBasicData().onSuccess {
                uiState = uiState.copy(basicData = it)
                AppStore.save("basicData", it.toJson())
            }
        }

        viewModelScope.launch {
            TuYaManager.requestMessageNew {
                uiState = uiState.copy(hasNewMsg = it)
            }
        }
    }





    fun getHomeDetail() {

        updateState(
            uiState.copy(
                loading = true
            )
        )

        viewModelScope.launch {
            TuYaManager.getHomeDetail(onSuccessCallback = {
                uiState = uiState.copy(
                    deviceList = it,
                    loading = false
                )
            }, onErrorCallback = {
                uiState = uiState.copy(
                    deviceList = emptyList(),
                    loading = false
                )
            })

        }

    }

    fun startActivator(
        context: Context,
        scanDeviceBean: ScanDeviceBean,
        ssid: String,
        passwd: String,
        onActivatorSuccess:(DeviceBean)->Unit
    ) {

        uiState =
            uiState.copy(dialogType = DialogType.LoadingDialog(context.getString(R.string.home_22)))

        TuYaManager.getActivatorToken(
            onSuccessCallback = { token,homeId ->

                TuYaManager.startActivator(
                    scanDeviceBean = scanDeviceBean,
                    ssid = ssid,
                    passwd = passwd,
                    token = token,
                    homeId = homeId,
                    onSuccessCallback = {
                        onActivatorSuccess(it)
                        updateState(uiState.copy(dialogType = DialogType.PairedResult(true)))
                    },
                    onErrorCallback = {
                        updateState(uiState.copy(dialogType = DialogType.PairedResult(false)))
                    })

            },
            onError = {
                Log.i("sss", "getActivatorToken: error = $it")
                uiState = uiState.copy(dialogType = DialogType.PairedResult(false))
            }
        )


    }


}